package generadorDescriptores.forms.edition;

import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class TreeBuilder {
	
	static JTree arbol;
	
	public static JTree getTree(Document doc){
		//BI NODE
		DefaultMutableTreeNode bi = new DefaultMutableTreeNode("bi");
		NodeList list = doc.getElementsByTagName("bi");
		Element biNode = (Element) list.item(0); 
		String label = biNode.getAttribute("label");
		DefaultMutableTreeNode biLabel = new DefaultMutableTreeNode("label = "+label);
		bi.add(biLabel);
		
		String name = biNode.getAttribute("name");
		DefaultMutableTreeNode biName = new DefaultMutableTreeNode("name = "+name);
		bi.add(biName);
		
		String privilege = biNode.getAttribute("privilege");
		DefaultMutableTreeNode biPrivilege = new DefaultMutableTreeNode("privilege = "+privilege);
		bi.add(biPrivilege);
		
		//FACTTABLE NODE
		DefaultMutableTreeNode facttable = new DefaultMutableTreeNode("facttable");
		bi.add(facttable);
		list = doc.getElementsByTagName("facttable");
		Element facttableNode = (Element) list.item(0); 
		String metricAsString = facttableNode.getAttribute("metric");
		DefaultMutableTreeNode metric = new DefaultMutableTreeNode("metric = "+metricAsString);
		facttable.add(metric);
		
		String tableMetric = facttableNode.getAttribute("table");
		DefaultMutableTreeNode facttableTable = new DefaultMutableTreeNode("table = "+tableMetric);
		facttable.add(facttableTable);
		
		String metricType = facttableNode.getAttribute("type");
		DefaultMutableTreeNode typeMetric = new DefaultMutableTreeNode("type = "+metricType);
		facttable.add(typeMetric);
		
		String formatter = facttableNode.getAttribute("formatter");
		DefaultMutableTreeNode formato = new DefaultMutableTreeNode("formatter = "+formatter);
		facttable.add(formato);
		if(formatter.contains("Currency")){
			String currencyType = facttableNode.getAttribute("currency");
			DefaultMutableTreeNode currency = new DefaultMutableTreeNode("currency = "+currencyType);
			facttable.add(currency);	
		}
		
		//DIMENSION NODE
		/*DefaultMutableTreeNode dimensions = new DefaultMutableTreeNode("dimensions");
		bi.add(dimensions);
		list = doc.getElementsByTagName("dimension");
		for (int i = 0; i < list.getLength(); i++) {
			Element dimNode = (Element) list.item(i);
			String dimensionName = dimNode.getAttribute("name");
			DefaultMutableTreeNode dimension = new DefaultMutableTreeNode("name = "+dimensionName);
			dimensions.add(dimension);
			NodeList properties = dimNode.getChildNodes();
			for (int j = 0; j < properties.getLength(); j++) {
				Element property = (Element) properties.item(j);
				String propertyName = property.getAttribute("name");
			}
		}*/
		
				
		
		
		DefaultMutableTreeNode joins = new DefaultMutableTreeNode("joins");
		bi.add(joins);
		
		arbol = new JTree(bi);
		return arbol; 
	}
	
	

}
